﻿using System;
using System.Windows.Forms;
using Me.Amon.C;
using Me.Amon.POS.M;
using Me.Amon.Util;
using Newtonsoft.Json;

namespace Me.Amon.POS.V
{
    public partial class WfCfg : Form
    {
        public WfCfg()
        {
            InitializeComponent();
        }

        private void WfCfg_Load(object sender, EventArgs e)
        {
            var settings = Me.Amon.Properties.Settings.Default;
            TbCorp.Text = settings.CorpCode;
            TbShop.Text = settings.ShopCode;
            TbCode.Text = settings.Code;
            TbServer.Text = settings.Server;
        }

        private void TbUpdate_Click(object sender, EventArgs e)
        {
            var corp = TbCorp.Text.Trim();
            if (string.IsNullOrWhiteSpace(corp))
            {
                MessageBox.Show("公司代码不能为空！");
                TbCorp.Focus();
                return;
            }

            var shop = TbShop.Text.Trim();
            if (string.IsNullOrWhiteSpace(shop))
            {
                MessageBox.Show("门店代码不能为空！");
                TbShop.Focus();
                return;
            }

            var code = TbCode.Text.Trim();
            if (string.IsNullOrWhiteSpace(code))
            {
                MessageBox.Show("终端代码不能为空！");
                TbCode.Focus();
                return;
            }

            var server = TbServer.Text.Trim().ToLower();
            //if (!Regex.IsMatch(server, "^(http://)?\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}(:\\d{1,5}/?)?$"))
            if (string.IsNullOrWhiteSpace(server))
            {
                MessageBox.Show("服务器地址格式错误！");
                TbServer.Focus();
                return;
            }

            //var match = Regex.Match(server, "\\d+");
            //while (match.Success)
            //{
            //    var val = int.Parse(match.Value);
            //    if (val > 255)
            //    {
            //        MessageBox.Show("IP地址应在0至255之间！");
            //        return;
            //    }
            //    match = match.NextMatch();
            //}
            if (!server.StartsWith("http://"))
            {
                server = "http://" + server;
            }
            if (server[server.Length - 1] == '/')
            {
                server = server.Substring(0, server.Length - 1);
            }

            var demo = string.Format("{0}/POS/POS.ashx?{1}={2}&t={3}&a={4}", server, PPos.PARAM_KEY_OPT, PPos.PARAM_VALUE_OPT_SELECT, code, WebUtil.GetIp());
            var rest = WebUtil.POST(demo, "");
            if (string.IsNullOrWhiteSpace(rest))
            {
                MessageBox.Show("系统异常，请与管理员联系！");
                return;
            }
            var obj = JsonConvert.DeserializeObject<MTerminal>(rest);
            if (obj.HasError())
            {
                MessageBox.Show(obj.GetError());
                return;
            }

            var settings = Me.Amon.Properties.Settings.Default;
            settings.CorpCode = corp;
            settings.ShopCode = shop;
            settings.Code = code;
            settings.Server = server;
            settings.Save();
            
            Close();
        }

        private void BtCancel_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
